package cn.cjlmonster.demo02.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import cn.cjlmonster.demo02.R;

/**
 * This project is Demo02
 * Created by cjl on 2021/12/5 2:53 下午.
 */
public class ItemFragment extends Fragment {

    private final String TAG;

    public static ItemFragment getInstance(int color, String title) {
        ItemFragment fragment = new ItemFragment(title);
        Bundle bundle = new Bundle();
        bundle.putInt("color", color);
        bundle.putString("title", title);
        fragment.setArguments(bundle);
        return fragment;
    }

    private ItemFragment(String tab) {
        this.TAG = tab;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        Log.i(TAG, "onAttach: ");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate: ");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.i(TAG, "onCreateView: ");
        View root = inflater.inflate(R.layout.fragment_item, container, false);
        initView(root);
        return root;
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.i(TAG, "onStart: ");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i(TAG, "onPause: ");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.i(TAG, "onStop: ");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i(TAG, "onDestroyView: ");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: ");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.i(TAG, "onDetach: ");
    }

    private void initView(View root) {
        TextView tv_msg = root.findViewById(R.id.tv_msg);
        Bundle bundle = getArguments();
        if (bundle == null) return;
        int color = bundle.getInt("color", Color.WHITE);
        String title = bundle.getString("title");
        root.setBackgroundColor(color);
        tv_msg.setText(title);
    }

}
